package de.fuberlin.dbs.wm;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.fuberlin.dbs.wm.dao.MatchDao;
import de.fuberlin.dbs.wm.dao.PlayerDao;
import de.fuberlin.dbs.wm.dao.TeamDao;
import de.fuberlin.dbs.wm.dao.model.Goal;
import de.fuberlin.dbs.wm.dao.util.DatabaseSource;
import de.fuberlin.dbs.wm.service.MatchService;
import de.fuberlin.dbs.wm.util.FreemarkerParser;
import de.fuberlin.dbs.wm.util.NotAllowedException;

import freemarker.template.TemplateException;

public class S_InsertGoal extends HttpServlet {	
	private static final long serialVersionUID = 4646599090007574512L;
	private final String template = "t_insertGoal.ftl";
	private PrintWriter print;
	@SuppressWarnings("rawtypes")
	private Map datamodel;
	private String tournament;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		print = response.getWriter();
		datamodel = new HashMap();
		
		try {
			/*Connect to the Oracle Database*/			
			DatabaseSource.setUpConnection();	
			tournament = request.getParameter("tournament");
			if(request.getParameter("sent") != null) {
				String foul = request.getParameter("foul") == null ? "0" : request.getParameter("foul");
				Goal goal = new Goal(
						Integer.parseInt(request.getParameter("min")),
						Integer.parseInt(request.getParameter("game")),
						request.getParameter("minPhase"),
						Integer.parseInt(request.getParameter("player")),
						Integer.parseInt(foul),
						request.getParameter("team"));
				try {
					MatchService.insertGoal(goal);
					datamodel.put("info", "Eintrag erfolgreich");
				} catch (NotAllowedException e) {
					datamodel.put("warning", "Keine Berechtigung dies zu tun!");
				} catch (Exception e) {
					datamodel.put("error", "Daten konnten nicht gespeichert werden:<br>" + e.getMessage());
					e.printStackTrace();
				}
				
			}
			setUp();
		} catch (SQLException e) {
			datamodel.put("con_error", "Konnte sich nicht mit der Datenbank verbinden");
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				/*Send the datamodel to the template to be printed in the browser*/
				new FreemarkerParser(template).freemarkerDo(print, datamodel, this.getServletContext());
				DatabaseSource.closeConnection();
			} catch (SQLException e) {
				System.err.println("Could not close Connection");
			} catch (NullPointerException e) {
				System.err.println("Connection could not be closed for not having opened");
			} catch (TemplateException e) {
				e.printStackTrace();
			}
		}	
	}
	
	@SuppressWarnings("unchecked")
	private void setUp() {		
		try {
			datamodel.put("players", PlayerDao.getAllPlayersOf(this.tournament));
			datamodel.put("games", MatchDao.getAllMatchesOf(this.tournament));
			datamodel.put("fouls", MatchDao.getFouls(tournament));
			datamodel.put("teams", TeamDao.getAll(tournament));
			List<String> phases = new ArrayList<String>();
			phases.add("1. HZ");
			phases.add("2. HZ");
			phases.add("1. VL");
			phases.add("2. VL");
			phases.add("11m");
			datamodel.put("phases", phases);
			datamodel.put("tournament", tournament);
		} catch (SQLException e) {
			System.err.println("Could not retrieve data from database");
			e.printStackTrace();
		}
		
	}
}
